package com.lottery.controller.qiuqiu.lobbyController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lottery.controller.BaseController;
import com.lottery.dto.PlaywayDto;
import com.lottery.dto.ResultDto;
import com.lottery.service.qiuqiu.lobbyService.LobbyService;
import com.lottery.utils.lobbyUtils.impl.BjscPlayUtilImpl;
import com.lottery.utils.lobbyUtils.impl.SscPlayutilImpl;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/api/lobby/")
public class LobbyController extends BaseController {
    @Resource
    private LobbyService lobbyService;

    /**
     * 用户下注列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/searchBetRecord", method = RequestMethod.POST, produces = {
            "application/json;charset=UTF-8"})
    public ResultDto searchBetRecord(HttpServletRequest request) {
        ResultDto result = new ResultDto();
        try {
            String body = readHttpBody(request);
            JSONObject obj = JSONObject.parseObject(body);
            String startTime = obj.getString("startTime");
            String endTime = obj.getString("endTime");
            String userId = obj.getString("userId");
            lobbyService.searchBetRecord(startTime, endTime, userId, result);
        } catch (IOException e) {
            logger.error("LobbyController searchBetRecord error,{}", e);
            result.setResultData(false, "获取信息失败", 1, null);
        }
        return result;
    }

    /**
     * 用户每一期下注详情
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/searchBetDetail", method = RequestMethod.POST, produces = {
            "application/json;charset=UTF-8"})
    public ResultDto searchWealthInfo(HttpServletRequest request) {
        ResultDto result = new ResultDto();
        try {
            String body = readHttpBody(request);
            JSONObject obj = JSONObject.parseObject(body);
            int userId = obj.getInteger("userId");
            String expect = obj.getString("expect");
            int lotteyType = obj.getInteger("lotteyType");
            String agencyMark = obj.getString("agencyMark");
            lobbyService.searchWealthInfo(userId, expect, lotteyType, agencyMark,result);
        } catch (Exception e) {
            logger.error("WealthController cancelWithdrawById error,{}", e);
            result.setResultData(false, "查询信息失败", 1, null);
        }
        return result;
    }


    /**
     * 游戏下注快捷方式
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/playway", method = RequestMethod.POST, produces = {
            "application/json;charset=UTF-8"})
    public ResultDto playway(HttpServletRequest request) {
        ResultDto result = new ResultDto();
        try {
            String body = readHttpBody(request);
            JSONObject obj = JSONObject.parseObject(body);
            Integer userId = obj.getInteger("userId");
            Integer lotteyType = obj.getInteger("lotteyType");
            String agencyMark = obj.getString("agencyMark");
            Integer loginType = obj.getInteger("loginType") == null ? 0 : obj.getInteger("loginType");
            List<PlaywayDto> list = JSON.parseArray(obj.getString("param"), PlaywayDto.class);
            if (lotteyType == 1) {
                lobbyService.Sscplayway(userId, lotteyType, agencyMark, list, loginType, result);
            } else if (lotteyType == 3) {
                lobbyService.bjscPlayWay(userId, lotteyType, agencyMark, list, loginType, result);
            }
        } catch (Exception e) {
            logger.error("Lobby Controller playway error,{}", e);
            result.setResultData(false, "查询信息失败", 1, null);
        }
        return result;
    }

    /**
     * 游戏下注输入框方式
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/playwayByZZ", method = RequestMethod.POST, produces = {
            "application/json;charset=UTF-8"})
    public ResultDto playwayByZZ(HttpServletRequest request) {
        ResultDto result = new ResultDto();
        try {
            List<PlaywayDto> list = new ArrayList<>();
            String body = readHttpBody(request);
            JSONObject obj = JSONObject.parseObject(body);
            Integer userId = obj.getInteger("userId");
            Integer lotteyType = obj.getInteger("lotteyType");
            String agencyMark = obj.getString("agencyMark");
            String param = obj.getString("param");
            Integer loginType = obj.getInteger("loginType") == null ? 0 : obj.getInteger("loginType");
            PlaywayDto playwayDto = null;
            //将输入下注规则封装
            if (lotteyType == 1) {
                playwayDto = SscPlayutilImpl.pacPlaywayInfo(param, agencyMark, lotteyType);
            } else if (lotteyType == 3) {
                playwayDto = BjscPlayUtilImpl.pacPlaywayInfo(param, agencyMark, lotteyType);
            }
            //下注
            if (playwayDto == null) {
                result.setResultData(false, "输入规则错误,请重新输入", 2, null);
            } else {
                list.add(playwayDto);
                if (lotteyType == 1) {
                    lobbyService.Sscplayway(userId, lotteyType, agencyMark, list, loginType, result);
                } else if (lotteyType == 3) {
                    lobbyService.bjscPlayWay(userId, lotteyType, agencyMark, list, loginType, result);
                }
            }
        } catch (Exception e) {
            logger.error("Lobby Controller playway error,{}", e);
            result.setResultData(false, "查询信息失败", 1, null);
        }
        return result;
    }


    //根据渠道商获取赔率信息
    @RequestMapping(value = "/searchOddByAgency", method = RequestMethod.POST, produces = {
            "application/json;charset=UTF-8"})
    public ResultDto searchOddByAgency(HttpServletRequest request) {
        ResultDto result = new ResultDto();
        try {
            String body = readHttpBody(request);
            JSONObject obj = JSONObject.parseObject(body);
            String agencyMark = obj.getString("agencyMark");
            Integer lotteyType = obj.getInteger("lotteyType");
            lobbyService.searchOddByAgency(agencyMark, lotteyType, result);
        } catch (Exception e) {
            logger.error("LobbyController searchOddByAgency error,{}", e);
            result.setResultData(false, "查询信息失败", 1, null);
        }
        return result;
    }

    /**
     * 查询彩票的开奖号码相关信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/querySscWinInfo", method = RequestMethod.POST, produces = {
            "application/json;charset=UTF-8"})
    public ResultDto querySscWinInfo(HttpServletRequest request) {
        ResultDto result = new ResultDto();
        try {
            String body = readHttpBody(request);
            JSONObject obj = JSONObject.parseObject(body);
            String expect = obj.getString("expect");
            Integer lotteyType = obj.getInteger("lotteyType");
            lobbyService.querySscWinInfo(expect, lotteyType, result);
        } catch (IOException e) {
            logger.error("LobbyController querySscWinInfo error,{}", e);
            result.setResultData(false, "查询信息失败", 1, null);
        }
        return result;
    }

    /**
     * 查询彩票这一期的竞猜记录
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryBetInfo", method = RequestMethod.POST, produces = {
            "application/json;charset=UTF-8"})
    public ResultDto queryBetInfo(HttpServletRequest request) {
        ResultDto result = new ResultDto();
        try {
            String body = readHttpBody(request);
            JSONObject obj = JSONObject.parseObject(body);
            String expect = obj.getString("expect");
            Integer userId = obj.getInteger("userId");
            Integer lotteyType = obj.getInteger("lotteyType");
            lobbyService.queryBetInfo(expect, userId, lotteyType, result);
        } catch (IOException e) {
            logger.error("LobbyController queryBetInfo error,{}", e);
            result.setResultData(false, "查询信息失败", 1, null);
        }
        return result;
    }

    /**
     * 用户取消下注
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/cancelBet", method = RequestMethod.POST, produces = {
            "application/json;charset=UTF-8"})
    public ResultDto cancelBet(HttpServletRequest request) {
        ResultDto result = new ResultDto();
        try {
            String body = readHttpBody(request);
            JSONObject obj = JSONObject.parseObject(body);
            String ids = obj.getString("ids");
            Integer userId = obj.getInteger("userId");
            lobbyService.cancelBet(ids, userId, result);
        } catch (Exception e) {
            logger.error("LobbyController cancelBet error,{}", e);
            result.setResultData(false, "查询信息失败", 1, null);
        }
        return result;
    }

    /**
     * 彩票以往下注记录查询
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/searchPastBetRecord", method = RequestMethod.POST, produces = {
            "application/json;charset=UTF-8"})
    public ResultDto searchPastBetRecord(HttpServletRequest request) {
        ResultDto result = new ResultDto();
        try {
            String body = readHttpBody(request);
            JSONObject obj = JSONObject.parseObject(body);
            String startTime = obj.getString("startTime");
            String endTime = obj.getString("endTime");
            Integer userId = obj.getInteger("userId");
            Integer lotteyType = obj.getInteger("lotteyType");
            lobbyService.searchPastBetRecord(startTime, endTime, userId, lotteyType, result);
        } catch (IOException e) {
            logger.error("LobbyController searchPastBetRecord error,{}", e);
            result.setResultData(false, "查询信息失败", 1, null);
        }
        return result;
    }

    /**
     * 根据用户查询下注月统计收益
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/serachForMouthCount", method = RequestMethod.POST, produces = {
            "application/json;charset=UTF-8"})
    public ResultDto serachForMouthCount(HttpServletRequest request) {
        ResultDto result = new ResultDto();
        try {
            String body = readHttpBody(request);
            JSONObject obj = JSONObject.parseObject(body);
            Integer userId = obj.getInteger("userId");
            Integer lotteyType = obj.getInteger("lotteyType");
            lobbyService.serachForMouthCount(userId, lotteyType, result);
        } catch (Exception e) {
            logger.error("LobbyController serachForMouthCount error,{}", e);
            result.setResultData(false, "查询信息失败", 1, null);
        }
        return result;
    }

    /**
     * 根据用户Id查询所有的竞猜记录(有分页处理信息)
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/searchAllbetRecordByUserID", method = RequestMethod.POST, produces = {
            "application/json;charset=UTF-8"})
    public ResultDto searchAllbetRecordByUserID(HttpServletRequest request) {
        ResultDto result = new ResultDto();
        try {
            String body = readHttpBody(request);
            JSONObject obj = JSONObject.parseObject(body);
            Integer userId = obj.getInteger("userId");
            Integer lotteyType = obj.getInteger("lotteyType");
            Integer pageNow = obj.getInteger("pageNow");
            Integer pageSize = obj.getInteger("pageSize");
            lobbyService.searchAllbetRecordByUserID(userId, lotteyType, pageNow, pageSize, result);
        } catch (Exception e) {
            logger.error("LobbyController searchAllbetRecordByUserID error,{}", e);
            result.setResultData(false, "查询信息失败", 1, null);
        }
        return result;
    }


    /**
     * 查询开奖走势
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/searchBetZoushi", method = RequestMethod.POST, produces = {
            "application/json;charset=UTF-8"})
    public ResultDto searchBetZoushi(HttpServletRequest request) {
        ResultDto result = new ResultDto();
        try {
            String body = readHttpBody(request);
            JSONObject obj = JSONObject.parseObject(body);
            Integer lotteyType = obj.getInteger("lotteyType");
            Integer pageSize = obj.getInteger("pageSize");
            lobbyService.searchBetZoushi(lotteyType, pageSize, result);
        } catch (Exception e) {
            logger.error("LobbyController searchBetZoushi error,{}", e);
            result.setResultData(false, "查询信息失败", 1, null);
        }
        return result;
    }


    /**
     * ssc长龙查询
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/searchSscChanglong", method = RequestMethod.POST, produces = {
            "application/json;charset=UTF-8"})
    public ResultDto searchSscChanglong(HttpServletRequest request) {
        ResultDto result = new ResultDto();
        try {
            String body = readHttpBody(request);
            JSONObject obj = JSONObject.parseObject(body);
            Integer lotteyType = obj.getInteger("lotteyType");
            lobbyService.searchSscChanglong(lotteyType, result);
        } catch (Exception e) {
            logger.error("LobbyController searchSscChanglong error,{}", e);
            result.setResultData(false, "查询长龙信息失败", 1, null);
        }
        return result;
    }
}
